<?php
if (!defined('APP') or !constant('APP')) die('Direct access not allowed!');
 
/*
 * Database Class Libray
 * Created date: 30-01-2012
 */

class Db 
{
    private static $db_host        = "localhost"; 
    private static $db_login       = "root";
    private static $db_password    = "";
    private static $db_name        = "testzabij";
    
    private static $db_PDO = null;
    
    /**
     *  Connect to database     
     */
    public static function connect()
    {
        
        if (self::$db_PDO != null) return;
        
        try
        {
            self::$db_PDO = new PDO("mysql:host=".self::$db_host.";dbname=".self::$db_name, self::$db_login, self::$db_password);                       
        }
        catch(PDOException $e) {         
            Log::save(Log::DB, $e->getMessage());
        }
    }
    
    public static function getPDO()
    {
        
        if (self::$db_PDO == null) self::connect(); 
        return self::$db_PDO;        
         
    }
    
    /**
     * Execute mysql query
     * @param String $query 
     * @return PDOStatement resultset
     */
    public static function query($query)
    {
        if (self::$db_PDO == null) self::connect();
        
        $_pdo = self::$db_PDO;
        
        try 
        {
            $_result = $_pdo->query($query);
        }
        catch (PDOException $e)
        {
            Log::save(Log::DB, $e->getMessage());
        }
        
        return $_result;        
    }
    
}

?>
